package com.cnap.multiclouldmgmt.util;

import com.cnap.multiclouldmgmt.controller.K8sClusterController;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author dyq
 * 工具类
 */
public class DateUtils {

    private final static Logger LOGGER = LoggerFactory.getLogger(DateUtils.class);


    /**
     *
     * @param beginDate
     * @param endDate
     * @return
     * @throws ParseException
     */
    public static int countSecondsBetweenTwoDate(String beginDate,String endDate) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");


        return (int)(((sdf.parse(endDate)).getTime() - (sdf.parse(beginDate)).getTime())/1000);
    }

    /**
     *
     * @param beginDate
     * @param endDate
     * @return
     * @throws ParseException
     */
    public static int countSecondsBetweenTwoDate(long beginDate,long endDate) throws ParseException {


        return (int)((endDate- beginDate)/1000);
    }

    /**
     *
     * @param date
     * @return
     */
    public static String changeDateToTZ(Date date){

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

        return sdf.format(date);
    }

    /**
     *
     * @param oldTime
     * @return
     * @throws ParseException
     */
    public static String countTimeBetweenNow(String oldTime) throws ParseException {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
        Date oldDate = simpleDateFormat.parse(
                oldTime.replace("T", "").replace("Z", " "));

        //2021-03-19T09:45:07Z 时间格式长这样（世界标准时间）改成东八区时间要加八小时
        long betweenTime = (new Date()).getTime() - (oldDate.getTime() + 8*60*60*1000);

        long days = betweenTime / (1000 * 60 * 60 * 24);
        long hours = (betweenTime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (betweenTime % (1000 * 60 * 60)) / (1000 * 60);
        long seconds = (betweenTime % (1000 * 60)) / 1000;

        StringBuilder result = new StringBuilder();
        if (days > 0){
            result.append(String.valueOf(days)).append("d");
        }
        if (hours > 0){
            result.append(String.valueOf(hours)).append("h");
        }
        if (minutes > 0){
            result.append(String.valueOf(minutes)).append("m");
        }
        /* 不显示秒一级
        if (seconds > 0){
            result.append(String.valueOf(seconds)).append("s");
        }

         */

        return result.toString();
    }

    /**
     *
     * @param oldDate
     * @return
     * @throws ParseException
     */
    public static String changeDateFormatYYYYMMDD(String oldDate) throws ParseException {

        if (oldDate == null){

            return null;
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date dateTime = simpleDateFormat.parse(oldDate);

        return simpleDateFormat.format(dateTime);
    }

    /**
     *
     * @param oldDate
     * @return
     * @throws ParseException
     */
    public static Date getDateByYYMMDDTHHMMSS(String oldDate) throws ParseException {

        if (oldDate == null){

            return null;
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

        //处理8小时时差问题
        return new Date(simpleDateFormat.parse(oldDate).getTime() + 8 * 60*60*1000);
    }

    /**
     *
     * @param oldDate
     * @return
     * @throws ParseException
     */
    public static String changeDateFormatMMDD(Date oldDate) throws ParseException {

        if (oldDate == null){

            return null;
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd");

        return simpleDateFormat.format(oldDate);
    }

    /**
     *
     * @param date
     * @return
     */
    public static String changeDateFormatYYYYMMDDHHMMSS(Date date) {

        if (date == null){
            return null;
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        return simpleDateFormat.format(date);
    }

    /**
     *
     * @param date
     * @return
     */
    public static String changeDateFormatYYYYMMDDHHMM(Date date) {
        if (date == null){
            return null;
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");

        return simpleDateFormat.format(date);
    }

    /**
     *
     * @param date
     * @return
     */
    public static String changeDateFormatYYYY_MM_DD_HH_MM(Date date) {
        if (date == null){
            return null;
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        return simpleDateFormat.format(date);
    }

    /**
     *
     * @param dateStr
     * @return
     */
    public static long changeDateFormatYYYYMMDDHHMMToTimestamp(String dateStr) throws ParseException {

        long result = 0L;

        if (StringUtils.isEmpty(dateStr)){
            return result;
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

        try{

            result = simpleDateFormat.parse(dateStr).getTime();
        }catch (Exception e){
            LOGGER.error("changeDateFormatYYYYMMDDHHMMToTimestamp faile dateStr={}",dateStr,e);
        }

        return result;
    }
}
